package com.yricky.memoria.ui.dialog

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDialog
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.ComposeView
import androidx.lifecycle.ViewTreeLifecycleOwner
import androidx.savedstate.ViewTreeSavedStateRegistryOwner
import com.yricky.memoria.ui.theme.ThemeInst

/**
 * @author Yricky
 * @date 2021/12/10
 */

fun showComposeDialog(context: Context, content:@Composable (AppCompatDialog)->Unit):Boolean{
    val activity: AppCompatActivity = context as? AppCompatActivity ?: return false
    val dialog: AppCompatDialog = AppCompatDialog(activity).apply {
        ViewTreeLifecycleOwner.set(window?.decorView ?:return false ,activity)
        ViewTreeSavedStateRegistryOwner.set(window?.decorView ?:return false ,activity)
        setContentView(ComposeView(activity).also {
            it.setContent{
                ThemeInst.Material {
                    content(this@apply)
                }

            }
        })
    }
    dialog.show()
    return true
}